import { defineStore } from 'pinia'

export interface CrawlerConfig {
  platform: string
  crawlerType: string
  loginType: string
  saveDataOption: string
  keywords: string
  crawlCount: number
  startPage: number
  concurrency: number
  enableComments: boolean
  enableSubComments: boolean
  enableMedia: boolean
  enableProxy: boolean
}

export interface CrawlerStatus {
  isRunning: boolean
  isPaused: boolean
  currentTask: string
  progress: number
  crawledCount: number
  successCount: number
  failedCount: number
  skippedCount: number
  speed: number
  elapsedTime: string
  estimatedTime: string
}

export interface AppState {
  config: CrawlerConfig
  status: CrawlerStatus
  logs: Array<{
    time: string
    level: 'info' | 'warn' | 'error'
    message: string
  }>
}

export const useAppStore = defineStore('app', {
  state: (): AppState => ({
    config: {
      platform: 'xhs',
      crawlerType: 'search',
      loginType: 'qrcode',
      saveDataOption: 'sqlite',
      keywords: '游戏,pan.quark.cn,资源分享',
      crawlCount: 200,
      startPage: 1,
      concurrency: 3,
      enableComments: true,
      enableSubComments: false,
      enableMedia: false,
      enableProxy: false,
    },
    status: {
      isRunning: false,
      isPaused: false,
      currentTask: '',
      progress: 0,
      crawledCount: 0,
      successCount: 0,
      failedCount: 0,
      skippedCount: 0,
      speed: 0,
      elapsedTime: '00:00',
      estimatedTime: '00:00',
    },
    logs: [
      { time: '15:42:36', level: 'info', message: '系统启动完成' },
      { time: '15:42:37', level: 'info', message: '等待用户配置任务...' },
    ],
  }),

  getters: {
    totalProcessed: (state) => 
      state.status.successCount + state.status.failedCount + state.status.skippedCount,
    
    successRate: (state) => {
      const total = state.status.successCount + state.status.failedCount
      return total > 0 ? (state.status.successCount / total * 100).toFixed(1) : '0'
    },
  },

  actions: {
    updateConfig(config: Partial<CrawlerConfig>) {
      this.config = { ...this.config, ...config }
    },

    updateStatus(status: Partial<CrawlerStatus>) {
      this.status = { ...this.status, ...status }
    },

    addLog(level: 'info' | 'warn' | 'error', message: string) {
      const time = new Date().toLocaleTimeString('zh-CN', { hour12: false })
      this.logs.push({ time, level, message })
      
      // 限制日志数量
      if (this.logs.length > 1000) {
        this.logs = this.logs.slice(-500)
      }
    },

    clearLogs() {
      this.logs = []
    },

    startCrawling() {
      this.status.isRunning = true
      this.status.isPaused = false
      this.addLog('info', `开始爬取${this.config.platform}数据...`)
    },

    pauseCrawling() {
      this.status.isPaused = true
      this.addLog('warn', '任务已暂停')
    },

    stopCrawling() {
      this.status.isRunning = false
      this.status.isPaused = false
      this.addLog('info', '任务已停止')
    },

    resetStatus() {
      this.status = {
        isRunning: false,
        isPaused: false,
        currentTask: '',
        progress: 0,
        crawledCount: 0,
        successCount: 0,
        failedCount: 0,
        skippedCount: 0,
        speed: 0,
        elapsedTime: '00:00',
        estimatedTime: '00:00',
      }
    },
  },
})